package com.yuandian.management.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandian.api.management.entity.ConfComponentIp;
import com.yuandian.api.management.entity.ConfSegment;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.security.util.SecurityUtils;
import com.yuandian.management.mapper.ConfComponentIpMapper;
import com.yuandian.management.service.ConfComponentIpService;
import com.yuandian.management.service.ConfSegmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 组件IP配置表-Service实现
 * @author zfsn
 * @date 2023-3-16
 */
@Service
public class ConfComponentIpServiceImpl extends ServiceImpl<ConfComponentIpMapper, ConfComponentIp>
		implements ConfComponentIpService {

	@Autowired
	ConfComponentIpMapper confComponentIpMapper;

	@Lazy
	@Autowired
	ConfSegmentService confSegmentService;

	@Override
	public List<ConfComponentIp> selectByIsFilter(int i) {
		return confComponentIpMapper.selectByIsFilter(i);
	}

	@Override
	public void batchUpdateSyncState(List<Integer> managementIdList, int i) {
		confComponentIpMapper.batchUpdateSyncState(managementIdList, i);
	}

	@Override
	public void resetIpFilter() {
		confComponentIpMapper.resetIpFilter();
	}

	/**
	 * 查询探针ip过滤分页信息
	 *
	 * @param confComponentIp
	 * @return 通用返回对象
	 */
	@Override
	public Page<ConfComponentIp> queryInfoFilterIp(ConfComponentIp confComponentIp) {
		return confComponentIpMapper.queryInfoFilterIp(new Page<>(confComponentIp.getPageNum(), confComponentIp.getPageSize()) ,confComponentIp);
	}

	/**
	 * 新增 ip过滤信息
	 *
	 * @param confComponentIp
	 * @return 通用返回对象
	 */
	@Override
	public R updateFilterIpInfo(ConfComponentIp confComponentIp) {
		int flag = 0;
		if (confComponentIp.getIsFilter() == 1) {
			flag = confComponentIpMapper.updateIpFilter(confComponentIp);
		} else {
			// 2.5的逻辑0的才更新状态
			confComponentIp.setSynchState(0);
			flag = confComponentIpMapper.updateIpFilterAndSynchStatus(confComponentIp);
		}
		return flag > 0 ? R.ok() : R.failed();
	}

	/**
	 * 分页查询
	 *
	 * @param confComponentIp 筛选条件
	 * @return 通用返回对象
	 */
	@Override
	public Page<ConfComponentIp> listPage(ConfComponentIp confComponentIp) {
		return confComponentIpMapper.listPage(new Page<>(confComponentIp.getPageNum(), confComponentIp.getPageSize()) ,confComponentIp);
	}

	/**
	 * 新增数据
	 *
	 * @param confComponentIp 实例对象
	 * @return 通用返回对象
	 */
	@Override
	public R<String> saveConfig(ConfComponentIp confComponentIp) {
		ConfSegment segment = confSegmentService.getById(confComponentIp.getSegmentId());
		if (segment != null) {
			confComponentIp.setCreateBy(SecurityUtils.getUser().getName());
			super.save(confComponentIp);
		}
		return R.ok();
	}
}